package edu.hawaii.senin.iclouds.simple;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

import edu.hawaii.senin.iclouds.basics.Atom;
import edu.hawaii.senin.iclouds.basics.Molecule;
import edu.hawaii.senin.iclouds.basics.PopulationException;
import edu.hawaii.senin.iclouds.basics.PopulationSimple;
import edu.hawaii.senin.iclouds.basics.ReadConfigException;

/**
 * Tests appropriate configuration parsing.
 * 
 * @author Pavel Senin.
 * 
 */
public class TestReactionPathway {

  private static final String configName1 = "config/test_cfg/test002.xml";
  private Cloud testCloud;

  /**
   * Makes testing environment setup.
   * 
   * @throws ReadConfigException if error encountered.
   * 
   */
  @Before
  public void setUp() throws ReadConfigException {
    this.testCloud = new Cloud();
    CloudParametersMessage cm = new CloudParametersMessage();
    cm.addParameter(CloudParametersMessage.CFGNAME, configName1);
    testCloud.setParameters(cm);
  }

  /**
   * Tests correct variables extraction and barrier calculation.
   * 
   * @throws CloudReactionsException when an error encountered.
   * 
   */
  @Test
  public void testHProcessing() throws CloudReactionsException {
    try {

      // Testing H processing - all H should evaporate
      PopulationSimple acrretionSample = new PopulationSimple();
      acrretionSample.addSpecie("H", new Atom("H", 1L), 7L);
      testCloud.runSurfaceCycle(acrretionSample);
      assertNull("Asserting H evaporation", testCloud.getGrainPopulation().surfaceGetSpecie("H"));

      // Testing CO accretion - all CO should stay
      acrretionSample = new PopulationSimple();
      acrretionSample.addSpecie("CO", new Molecule("CO", 1L), 1L);
      testCloud.runSurfaceCycle(acrretionSample);
      assertEquals("Asserting CO accretion", 1L, testCloud.getGrainPopulation().surfaceGetSpecie(
          "CO").getAccumulatorValue());
      acrretionSample.addSpecie("CO", new Molecule("CO", 7L), 7L);
      testCloud.runSurfaceCycle(acrretionSample);
      assertEquals("Asserting CO accretion", 8L, testCloud.getGrainPopulation().surfaceGetSpecie(
          "CO").getAccumulatorValue());

      // // Testing HCO formation
      // acrretionSample = new PopulationSimple();
      // acrretionSample.addSpecie("H", new Atom("H", 1L), 1L);
      // testCloud.runSurfaceCycle(acrretionSample);
      // assertEquals("Asserting CO accretion", 1L, testCloud.getGrainPopulation().surfaceGetSpecie(
      // "HCO").getAccumulatorValue());
      //
      // // Testing HCO formation
      // acrretionSample = new PopulationSimple();
      // acrretionSample.addSpecie("H", new Atom("H", 1L), 1L);
      // testCloud.runSurfaceCycle(acrretionSample);
      // assertEquals("Asserting CO accretion", 2L, testCloud.getGrainPopulation().surfaceGetSpecie(
      //          "HCO").getAccumulatorValue());

    }
    catch (PopulationException e) {
      fail("Exception generated:\n" + e.getMessage());
    }
  }

}
